package com.qimou.iems.controller;

import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.QualityCategory;
import com.qimou.iems.service.IQualityCategoryService;
import com.qimou.iems.util.CommonUtil;
import com.qimou.iems.util.StatusConstant;
import com.qimou.iems.util.ViewData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/6/3 21:34.
 **/
@RestController
@RequestMapping(value = "/qualityCategory")
public class QualityCategoryController extends BaseController {

    @Resource
    private IQualityCategoryService qualityCategoryService;




    /**
     */
    @RequestMapping(value = "/getAll")
    public ViewData getAll(){
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"获取成功",
                qualityCategoryService.getQualityCategory(null));
    }


    /**
     * 获取列表
     * @param pageArgs
     * @param categoryName
     * @return
     */
    @RequestMapping(value = "/getList")
    public ViewData getList(PageArgs pageArgs,String categoryName){
        categoryName = CommonUtil.isEmpty(categoryName) ? null : categoryName;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("categoryName",categoryName);
        PageList<QualityCategory> data = qualityCategoryService.getQualityCategory(pageArgs, map);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                data.getTotalSize(),data.getList());
    }


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ViewData save(String categoryName){
        if(CommonUtil.isEmpty(categoryName)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数不能为空");
        }
        QualityCategory qualityCategory = new QualityCategory();
        qualityCategory.setCategoryName(categoryName);
        qualityCategoryService.save(qualityCategory);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ViewData update(QualityCategory qualityCategory){
        if(null != qualityCategory.getId()){
            qualityCategoryService.update(qualityCategory);
        }
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


}
